#include "readfile.h"
#include "ui_readfile.h"
#include <QStandardItemModel>
#include <QDebug>
#include <QMessageBox>

Readfile::Readfile(QHash<QString,DEV> dev,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Readfile)
{
    ui->setupUi(this);
    init();



    QHash<QString,DEV>::iterator i;
    int j  = 0;

    QStandardItemModel* model = new QStandardItemModel();
    model->setColumnCount(1);
    model->setHeaderData(0,Qt::Horizontal,QString("    Device No    "));

    QStandardItem* item = 0;

     //qDebug() << "遍历哈希表如下-------------" << endl;
     for(i = dev.begin(); i != dev.end(); ++i,j++) {
         qDebug() << i.key() <<"        " << i.value().ID << i.value().status;

         item = new QStandardItem(QString("%1").arg(i.value().ID));
         item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
         model->setItem(j,0,item);
     }


     ui->tableView->setModel(model);
     ui->tableView->show();
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
     ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void Readfile::recvdev(QString str,QHash<QString,DEV> dev)
{
    language = str;
    QHash<QString,DEV>::iterator i;
    int j  = 0;

     ui->tableView->clearSpans();

    QStandardItemModel* model = new QStandardItemModel();
    model->setColumnCount(1);
    model->setHeaderData(0,Qt::Horizontal,QString("    Device No    "));

    QStandardItem* item = 0;

     //qDebug() << "遍历哈希表如下-------------" << endl;
     for(i = dev.begin(); i != dev.end(); ++i,j++) {
         qDebug() << i.key() <<"        " << i.value().ID << i.value().status;

         item = new QStandardItem(QString("%1").arg(i.value().ID));
         item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
         model->setItem(j,0,item);
     }


     ui->tableView->setModel(model);
     ui->tableView->show();
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
     ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}


void Readfile::init()
{
    ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
    RightClick = new QMenu(ui->tableView);                        //右键点击菜单
    selectAction = new QAction(QString("Select"),this);     //删除事件
    RightClick->addAction(selectAction);
    //connect(ui->tableView,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(RightClickSlot(QPoint)));
    connect(ui->tableView,SIGNAL(clicked(const QModelIndex &)), this, SLOT(onTableClicked(const QModelIndex &)));
    connect(RightClick,SIGNAL(triggered(QAction*)),this,SLOT(RightClickDelete(QAction*)));
}

void Readfile::onTableClicked(const QModelIndex &index)
{
    qDebug() << index.row();
    iselectcRow = index.row();
    if (index.isValid()) {
        QString cellText = index.data().toString();
        qDebug() << cellText;
         RightClick->exec(QCursor::pos());
    }
}

void Readfile::RightClickDelete(QAction *act)
{
    if (language == "English") {
        QMessageBox message(QMessageBox::NoIcon, QString("prompt"),
                                    QString("Do you choose the bank data?"),
        QMessageBox::Yes | QMessageBox::No, NULL);

        if(message.exec() != QMessageBox::Yes)
            return;

    }

    if (language == "Chinese") {
        QMessageBox message(QMessageBox::NoIcon, QString("提示"),
                                    QString("选择此设备?"),
        QMessageBox::Yes | QMessageBox::No, NULL);

        if(message.exec() != QMessageBox::Yes)
            return;

    }

    //if(message.exec() == QMessageBox::Yes)
    {
        if(act->text() == QString("Select"))   //看选中了删除这个菜单
        {
            qDebug() << "yes" << iselectcRow;
            emit sendId(iselectcRow);
            //this->deleteLater();
            this->hide();
        }
    }
}

void Readfile::RightClickSlot(QPoint pos)
{
    QModelIndex index = ui->tableView->indexAt(pos);    //找到tableview当前位置信息
    iselectcRow = index.row();    //获取到了当前右键所选的行数

    if(index.isValid())        //如果行数有效，则显示菜单
    {
        RightClick->exec(QCursor::pos());
    }
}

Readfile::~Readfile()
{
    delete ui;
}

void Readfile::closeEvent ( QCloseEvent * e )
{
    //qDebug() << "关闭";
    emit wclose();
}
